<template>
  <div>
    <!-- 标题 -->
    <child-nav-bar title="学习成果认定审核" beforePath="/staging" placeholder="请输入班级" :queryParams="queryParams"  @handleQuery="handleQuery"></child-nav-bar>

    <!-- 学生列表 -->
    <van-swipe-cell v-for="item in achievementList" :key="item.id">
      <van-card :title="item.nickname" :thumb="'http://localhost:8015/user/'+ item.uploadFile">
        <!-- 学院信息 -->
        <template #desc>
          <div style="color: darkgray">
            {{item.departmentName}}
            {{item.majorName}}
          </div>
          <div style="color: #0b790b">
            {{item.describe}}
          </div>
        </template>
        <!-- 学生联系方式 -->
        <template #price>
          <div>
            {{item.s_number}}
          </div>
        </template>
        <!-- 学生的账号管理状态 -->
        <template #price-top>
          <div>
            <span v-show="item.outcome === '0'" style="color: rgba(255, 0, 0, 0.539);">未通过</span>
            <span v-show="item.outcome === '1'">审批中</span>
            <span v-show="item.outcome === '2'" style="color: rgba(0, 128, 0, 0.62);">通过</span>
          </div>
        </template>
        <template #num>
          {{item.achievement_type.type_name}}
        </template>
      </van-card>
      <template #right>
        <van-button square text="赋值" @click="modification(item,2)" :disabled="item.outcome!=='1'" type="primary" class="delete-button" />
        <br>
        <van-button square text="驳回" @click="modification(item,0)" :disabled="item.outcome!=='1'" type="danger" class="delete-button" />
      </template>
    </van-swipe-cell>

    <!-- 加载更多 -->
    <div v-if="achievementList.length < total" @click="search(queryParams.pageSize+=10)" style="text-align: center;color: darkgray">
      点击加载更多
    </div>
    <van-popup v-model="achievementDialog.show" position="bottom" :style="{ height: '30%' }" >
      <van-form @submit="onSubmit">
        <van-field v-model="achievement.reason" label="驳回" v-if="achievementDialog.category === 0" placeholder="请输入驳回的原因"/>
        <van-field v-model="achievement.scores" label="赋分" v-if="achievementDialog.category === 2" placeholder="请输入要赋予的分数"/>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </van-popup>
  </div>
</template>

<script>
import {affirmAchievementFail, affirmAchievementPass, pagingAffirm} from "@/api/teacher/pupil";
import {Notify} from "vant";
import {getMajorsByDepartmentId} from "@/api/user";

export default {
  name: "index",
  data(){
    return{
      // 查询出来的总条数
      total:0,
      achievementList:[],
      queryParams:{
        pageNum: 1, // 当前页数
        pageSize: 10, // 查询的条数
        majorId:undefined, // 专业id
        stuClass:undefined,
        departmentId:this.$store.getters.info.departmentId, // 学院Id
        queryItems:[] //单选框
      },
      username:'',
      // 驳回弹框
      achievementDialog:{
        show:false,
        category:2 // 2-赋值 0-驳回
      },
      achievement:{
        id:'', // 教师Id
        t_number:'', // 教师工号
        outcome:'', // 审核结果
        reason: '', // 驳回
        scores: '', // 赋分
        reviewTime:'' // 审核时间
      }
    }
  },
  created() {
    this.search();
    this.getMajorsList();
  },
  methods:{
    /**
     * 获取当前学院的所有专业信息
     * */
    async getMajorsList(){
      let res = await getMajorsByDepartmentId(this.queryParams.departmentId);
      let list = res.data.map( item =>{
        return{
          label: item.majorName,
          value: item.majorId
        }
      })
      this.queryParams.queryItems = [{
        label:'专业',
        value:'majorId',
        children: list
      }]
    },
    handleQuery(item){
      // 默认从第一页开始查
      this.queryParams.pageNum = 1;
      // 获取子组件传过来的值
      this.queryParams.stuClass = item.queryInput
      this.search();
    },
    /**
     * 查询学习成果列表
     * */
    async search(){
      let res = await pagingAffirm({ ...this.queryParams , queryItems: [] });
      this.achievementList = res.data.list;
      this.total = res.data.total;
    },
    /**
     * 学生状态修改事件
     * 0 - 驳回
     * 2 - 通过
     * */
    async modification(item,status){
      // 设置弹框内容
      this.achievementDialog.show = true;
      this.achievementDialog.category = status;
      // 赋值
      this.achievement.id = item.id;
      this.achievement.t_number = this.$store.getters.info.nickname;
    },
    /**
     * 提交赋值事件
     * */
    async onSubmit(){
      let res = null;
      // 上传时间
      this.achievement.reviewTime = new Date();
      if (this.achievement.scores !== ''){
        // 通过
        this.achievement.outcome = 2;
        res = await affirmAchievementPass(this.achievement);
      }else if (this.achievement.reason !== ''){
        // 驳回
        this.achievement.outcome = 0;
        res = await affirmAchievementFail(this.achievement);
      }
      // 刷新页面
      await this.search();
      // 提示信息
      Notify({ type: 'success', message: '成功'});
      // 清空数据
      this.achievement = {};
      // 关闭弹框
      this.achievementDialog.show = false;
    }
  }
}
</script>

<style scoped lang="scss">

</style>
